package isp.apps.example.shoppingcart.domain;

import java.math.BigDecimal;

import java.util.Iterator;
import java.util.List;

public class SimpleShippingCalculator implements ShippingCalculator
{
  private BigDecimal rate;
  private int itemCount = 0;
  
  public SimpleShippingCalculator()
  {
  }

  /**
   * When Given a list of CartItems and a Shipping Option object,
   * will return the cost to ship based on the shipping option
   * @param cartList
   * @param so
   * @return
   */
  public BigDecimal getShippingCost(List cartList, ShippingOption so)
  {
    itemCount = 0;
    CartItem ci = new CartItem();
    for (Iterator i = cartList.iterator(); i.hasNext(); )
    {
      ci = (CartItem)i.next();
      itemCount = itemCount + ci.getQuantity();
    }
    
    rate = so.getRate();
    return rate.multiply(new BigDecimal(itemCount));
  }
}
